local sx, sy = guiGetScreenSize() 
local zoom = sx < 1920 and math.min(2, 1920 / sx) or 1
local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
loadstring(exports.dgs:dgsImportFunction())()
local icons = {
    admin5 = dxCreateTexture('icons/rcon.png', 'argb', false, 'clamp'),
    admin2 = dxCreateTexture('icons/moderator.png', 'argb', false, 'clamp'),
    admin3 = dxCreateTexture('icons/admin.png', 'argb', false, 'clamp'),
    admin1 = dxCreateTexture('icons/support.png', 'argb', false, 'clamp'),
    admin4 = dxCreateTexture('icons/rcon.png', 'argb', false, 'clamp'),
    afk = dxCreateTexture('icons/afk.png', 'argb', false, 'clamp'),
    vip = dxCreateTexture('icons/vip.png', 'argb', false, 'clamp'),
    write = dxCreateTexture('icons/write.png', 'argb', false, 'clamp'),
}

f = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 16*px )

insertIcons=function(player)
    local items={}
    
    if(getElementData(player, "player:writing"))then
	    table.insert(items, "write")
    end

	if(getElementData(player, "player:admin"))then
		table.insert(items, "admin"..getElementData(player, "player:level"))
    end

	if(getElementData(player, "player:afk"))then
		table.insert(items, "afk")
    end
    
    if(getElementData(player, "player:vip"))then
		table.insert(items, "vip")
	end

	return items
end

function nametagRender()
    if(isChatBoxInputActive() and not getElementData(localPlayer, "player:writing"))then
		setElementData(localPlayer, "player:writing", true)
	elseif(not isChatBoxInputActive() and getElementData(localPlayer, "player:writing"))then
		setElementData(localPlayer, "player:writing", false)
    end

    local x,y,z = getElementPosition(localPlayer)
    local elements = getElementsWithinRange(x,y,z, 20, 'player')
    for i,v in ipairs(elements) do 
        if getElementDimension(v) == getElementDimension(localPlayer) and getElementInterior(v) == getElementInterior(localPlayer) then 
            if v ~= localPlayer then 
                if getElementAlpha(v) ~= 255 then return end 
                local x2,y2,z2 = getPedBonePosition(v, 6)
                local scx, scy = getScreenFromWorldPosition(x2,y2,z2+0.3)
                local distance = getDistanceBetweenPoints3D(x,y,z,x2,y2,z2+0.3)
                local dis = getEasingValue(1-distance/15, "InOutQuad")
                local dis2 = getEasingValue(1-distance/25, "InOutQuad")
                local scale = dis2*1
                local alpha = dis*222
                if distance <= 25 then 
                    if scx and scy then 
                        if distance <= 10 then 
                            local items=insertIcons(v)
                            local x=(16/zoom)*#items
                            for i,v in pairs(items) do
                                local sX=(32/zoom)*(i-1)
                                if icons[v] then 
                                    dxDrawImage(scx-(x-sX)*scale, scy-(45/zoom)*scale, (32/zoom)*scale, (32/zoom)*scale, icons[v], 0, 0, 0, tocolor(255, 255, 255, alpha))
                                end
                            end
                        end
                        dxDrawText("["..(getElementData(v, "id") or 0).."] "..getPlayerName(v).."", scx+1, scy+17+1, scx+1, scy+17+1, tocolor(0, 0, 0, alpha), 1-(distance/20), f, 'center', 'center', false, false, false, false)
                        dxDrawText("#d1d1d1["..(getElementData(v, 'id') or 0).."#d1d1d1] "..getPlayerName(v).."", scx, scy+17, scx, scy+17, tocolor(222, 222, 222, alpha), 1-(distance/20), f, 'center', 'center', false, false, false, true)            
                    end
                end
            end
        end 
    end
end

function setNametagState(bool)
    if bool then 
        removeEventHandler('onClientRender', getRootElement(), nametagRender)
        addEventHandler('onClientRender', getRootElement(), nametagRender)
    else
        removeEventHandler('onClientRender', getRootElement(), nametagRender)
    end
end

addEventHandler('onClientRender', getRootElement(), nametagRender)

function remotePlayerJoin()
    setPlayerNametagShowing(source, false)
end
addEventHandler("onClientPlayerJoin", getRootElement(), remotePlayerJoin)